<template>
  <div class="app-container customer-div">
    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="品名：">
          <el-select
            clearable
            v-model="listQuery.goodsName">
            <el-option
              v-for="item in options2"
              :key="item.goodsName"
              :label="item.goodsName"
              :value="item.goodsName">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="牌号：">
          <el-select
                  clearable
                  v-model="listQuery.goodsKingId"
                  filterable
                  remote
                  reserve-keyword
                  placeholder="请输入关键词"
                  :loading="loading"
                  :remote-method="remoteMethod">
            <el-option
                    v-for="(item,index) in options"
                    :key="index"
                    :label="item.goodsSn"
                    :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库：">
          <el-select v-model="listQuery.warehouseName" filterable placeholder="请选择" clearable>
            <el-option
                    v-for="item in wList"
                    :key="item.warehouseName"
                    :label="item.warehouseName"
                    :value="item.warehouseName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
                  @click="handleSearchList()"
                  type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--数据列表-->
    <div class="table-container">
      <el-table
              :header-cell-style="{'text-align':'center'}"
              v-loading="listLoading"
              :data="tableData"
              border
              style="width: 100%; margin-top: 20px">
        <el-table-column
                prop="goodsName"
                label="品名"
                min-width="100"
                align="center">
        </el-table-column>
        <el-table-column  label="牌号" min-width="150" align="center" prop="goodsSn">
        </el-table-column>
        <el-table-column
          sortable
          prop="enableNum"
          align="center"
          class-name="nopadding"
          min-width="100">
          <template slot="header" slot-scope="scope">
            <div style="text-align: center;display: inline-block">
              可用库存 <el-tooltip class="item" effect="dark" content="含待入库数量" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
            </div>
          </template>
        </el-table-column>
        <el-table-column min-width="150" class-name="nopadding" align="left">
          <template slot="header" slot-scope="scope">
            <div style="text-align: center">
              实际 <el-tooltip class="item" effect="dark" content="仓库在库数量" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
              <br/>库存数量
            </div>
          </template>
          <template slot-scope="scope">
              <div style="color:red;text-align: left;" :style="{borderTop:index == 0 ? '0' : '1px solid #EBEEF5'}" v-for="(item,index) in scope.row.stockList" v-show="item.stockNum > 0" v-if="scope.row.stockList.length > 1">
                <span style="text-align:left;width:150px;display: inline-block;padding-right:5px;padding-left:15px;">{{item.warehouseName}}</span><span>{{item.stockNum}}</span>
              </div>
              <div style="color:red;text-align: left" :style="{borderTop:index == 0 ? '0' : '1px solid #EBEEF5'}" v-for="(item,index) in scope.row.stockList" v-else>
                <span style="text-align:left;width:150px;display: inline-block;padding-right:5px;padding-left:15px;">{{item.warehouseName}}</span><span>{{item.stockNum}}</span>
              </div>
              <div :style="{borderTop:scope.row.showList.length > 0  ? '1px solid #EBEEF5' :'0' }"  v-if="scope.row.stockList.length > 1">
                <span style="width:94px;display: inline-block;font-weight:bold;color:#000;padding-left:15px;">合计</span> <span style="font-weight:bold;">{{ scope.row.stockList | fiflist}}</span>
              </div>
          </template>
        </el-table-column>

      </el-table>
    </div>
  </div>
</template>
<script>
  import {getCustomerOption,getOrderUsersList,purchaseOrderGoodsList,supplierList,getGoodsClassList,querySaleGoodsStock} from '@/api/partner'
  import {goodsList} from '@/api/company'
  import {getWarehouseList} from '@/api/order'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import { mapGetters } from 'vuex'
  const defaultListQuery={
    "kingId": "",
    "warehouseName": "",
    "goodsName": "",
    sortBy:'',
    "pageNum": 1,
    "pageSize": 10,
  };
  export default {
    name: "index",
    data(){
      return {
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        tableData: [],
        listLoading: false,
        roles:store.getters.roles ? store.getters.roles : [],
        loading:false,
        options:[],
        count:0,
        wList:[],
        options2:[],
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId',
        'isMange',
      ]),
      exportSaleInvoice(){
        if(this.roles.indexOf("exportSaleInvoice") != -1){
          return true
        }else{
          return false
        }
      },
      allSaleInvoice(){
        if(this.roles.indexOf("allSaleInvoice") != -1){
          return true
        }else{
          return false
        }
      },
    },
    created() {
      this.remoteMethod3();
      this.getWList()
      // this.getList();

    },
    filters: {
      fiflist(list){
        let sum = 0;
        list.forEach((el)=>{
          sum = sum + parseFloat(el.stockNum)
        });
        return parseFloat(sum).toFixed(4)
      },


    },
    methods:{
      getWList(){
        getWarehouseList().then((res)=>{
          this.wList = res
        })
      },
      //获取品类列表
      remoteMethod3() {
        getGoodsClassList().then((res)=>{
          this.options2 = res;
        }).catch(()=>{
        });
      },

      //商品
      remoteMethod(query) {
        if (query !== '') {
          this.loading = true;
          this.loading = false;
          goodsList({keywords:query}).then((res)=>{
            this.options = res.list;
          })
        } else {
          this.options = [];
        }
      },
      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },

      handleSearchList(){
        this.listQuery.pageNum = 1;
        this.getList();
      },

      //获取列表
      getList() {
        this.listLoading = true;
        querySaleGoodsStock(this.listQuery).then(response => {
          this.tableData = [];
          response.list.forEach((el,index)=>{
            el.showList = [];
            if(el.stockList.length > 1){
              el.showList = el.stockList.filter((ee)=>{
                return ee.stockNum > 0
              });
            }else{
              el.showList = JSON.parse(JSON.stringify(el.stockList))
            }
            this.tableData.push(el)
          });
          // this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
      },
    },
  }
</script>

<style scoped lang="scss">

</style>
